package com.jvyou.thread.test02;

/**
 * @author 橘柚
 * @version 1.0-SNAPSHOT
 * @Date 2024/7/31 15:40
 * @Description
 */
public class SendTask implements Runnable {

    // 剩余的礼品
    private volatile int left = 100;

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (left < 10) {
                    System.out.print(Thread.currentThread().getName() + ":");
                    System.out.println("剩余礼品数量不足，停止发送,剩余数量" + left);
                    break;
                } else {
                    left--;
                    System.out.println(Thread.currentThread().getName() + "送出1分礼物，剩余数量" + left);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        SendTask sendTask = new SendTask();

        Thread t1 = new Thread(sendTask,"多来A梦");
        Thread t2 = new Thread(sendTask,"大雄");
        t1.start();
        t2.start();
    }
}
